package com.wyz.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyz.entity.StudentEntity;
import com.wyz.mapper.StudentMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wk
 * @desc
 * @date 2024/12/22 17:47
 */
@Service
public class StudentService {
    @Resource
    private StudentMapper studentMapper;
    //分页查询所有学生信息
    public IPage<StudentEntity> getStudentListByPage(int currentPage) {
        IPage<StudentEntity> page = new Page<>(currentPage,5);
        IPage<StudentEntity> studentListPage = studentMapper.selectPage(page,null);
        return studentListPage;
    }
   //添加学生信息
    public boolean addStudent(StudentEntity studentEntity) {
        int insert = studentMapper.insert(studentEntity);
        return insert>0;
    }

    public boolean updateStudent(StudentEntity studentEntity) {
        return studentMapper.updateById(studentEntity)>0;
    }

    public StudentEntity queryStudentById(Integer studentId) {
        return studentMapper.selectById(studentId);
    }

    public boolean deleteStudent(Integer studentId) {
        return studentMapper.deleteById(studentId)>0;
    }

    public List<StudentEntity> queryStudentByNameOrId(String keyword) {
        return studentMapper.queryStudentByNameOrId(keyword);
    }

    public boolean addStudentToClass(List<Integer> studentsId, String className) {
        StudentEntity studentEntity= new StudentEntity();
        studentEntity.setStudentClass(className);
        UpdateWrapper<StudentEntity> wrapper = new UpdateWrapper<>();
        wrapper.in("student_id", studentsId);
        int rows = studentMapper.update(studentEntity, wrapper);
        return rows>0;
    }
}
